package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.mapper.StatsToIdMapper;
import com.hbnu.system.model.dto.StatsToIdDTO;
import com.hbnu.system.model.entity.Dept;
import com.hbnu.system.model.entity.User;
import com.hbnu.system.service.IDeptService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.service.StatsToIdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class StatsToIdImpl extends ServiceImpl< StatsToIdMapper, StatsToIdDTO> implements StatsToIdService {

    @Autowired
    private StatsToIdMapper statsToIdMapper;

    @Autowired
    IUserService iUserService;

    @Autowired
    IDeptService iDeptService;

    @Override
    public String AccountToId(String account){
        User user = iUserService.getOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getAccount, account));
        return user != null ? user.getUid() : "";
    }

    @Override
    public String AllauthorAccouhtToId(String allauthorAccouht){
        int[] result = new int[0];
        if (allauthorAccouht != null && !allauthorAccouht.isEmpty()) {
            // 对输入字符串进行处理，将非零数字提取出来存入数组
            result = Arrays.stream(allauthorAccouht.split(","))
                    .mapToInt(Integer::parseInt)
                    .filter(num -> num != 0)
                    .toArray();
        }

        List<String> authorNamesid = new ArrayList<>();
        // 遍历结果数组，将每个用户 ID 对应的姓名添加到列表中
        for (int one : result) {
            String o = String.valueOf(one);
            User u = iUserService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getAccount, o));
            if (u != null) {
                authorNamesid.add(u.getUid());
            }
        }

        String allauthorNameId=(String.join(",", authorNamesid));
        return allauthorNameId;
    }



    @Override
    public String DeptToId(String dept){
        Dept deptEntity = iDeptService.getOne(Wrappers.lambdaQuery(Dept.class)
                .eq(Dept::getDeptName, dept));
        return deptEntity != null ? deptEntity.getDeptID() : "";
    }
}
